package com.allways.dubbo.mdm.service.region.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.allways.dubbo.mdm.exception.MdmException;
import com.allways.dubbo.mdm.exception.MdmExceptionCode;
import com.allways.dubbo.mdm.service.BaseDataUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import com.allways.dubbo.base.dao.MdmBaseDaoImpl;
import com.allways.dubbo.mdm.beans.region.Region;
import com.allways.dubbo.mdm.service.region.IRegionService;

/**
 * <p>ClassName: IRegionService</p>
 * <p>Description: 行政区域Service接口</p>
 * <p>Author: weih</p>
 * <p>Date: 2016-10-11</p>
 */
@Service("mdmRegionService")
public class RegionServiceImpl extends MdmBaseDaoImpl implements IRegionService {
	Log log = LogFactory.getLog(RegionServiceImpl.class);

    @Autowired
    BaseDataUtils baseDataUtils;
    
    @Override
    public int addRegion(Region region) {
   		log.debug("For the new region parameters:"+ region.toJson());
   		validColumnLen(region);
    	
        //无code的不能插入
        if(StringUtils.isEmpty(region.getRegionCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        //code重复的不允许插入
        if(null != getRegionByCode(region.getRegionCode())){
            throw new MdmException(MdmExceptionCode.CODE_REPEATED,null);
        }
        
        return insert("region.insertRegion",region);
    }

    @Override
    public int updateRegion(Region region) {
    	log.debug("Update for the region parameters:"+ region.toJson());
    	validColumnLen(region);
     	//无code的不能更新
        if(StringUtils.isEmpty(region.getRegionCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        //code记录不存在
        Region oldRegion = getRegionByCode(region.getRegionCode());
        if(null == oldRegion){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_RESULT,null);
        }
		oldRegion.setId(region.getId());
		oldRegion.setRegionCode(region.getRegionCode());
		oldRegion.setRegionName(region.getRegionName());
		oldRegion.setParentId(region.getParentId());
		oldRegion.setStatus(region.getStatus());
		oldRegion.setRegionType(region.getRegionType());
		oldRegion.setPinyin(region.getPinyin());
		oldRegion.setQuanpin(region.getQuanpin());
		oldRegion.setLatitude(region.getLatitude());
		oldRegion.setLongitude(region.getLongitude());
		oldRegion.setRemark(region.getRemark());
		oldRegion.setDataSource(region.getDataSource());
		oldRegion.setIsDisabled(region.getIsDisabled());
		oldRegion.setCreatedBy(region.getCreatedBy());
		oldRegion.setCreatedTime(region.getCreatedTime());
		oldRegion.setUpdatedBy(region.getUpdatedBy());
		oldRegion.setUpdatedTime(region.getUpdatedTime());
        
        return update("region.updateRegion",oldRegion);
    }

    @Override
    public int deleteRegion(Region region) {
    	log.debug("Delete for the region parameters:"+ region.toJson());
     	//无code的不能删除
        if(StringUtils.isEmpty(region.getRegionCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        
        return delete("region.deleteRegion",region);
    }

    @Override
    public Region getRegionByCode(String code) {
    	log.debug("Query for region code:" + code);
        return queryForObject("region.getRegionByCode",code);
    }
    @Override
    public long getRegionCount(Region region) {
    	log.debug("Query for region count parameters:" + region.toJson());
        return queryForObject("region.getRegionCount",region);
    }
   @Override
    public List<Region> queryRegions(Region region, int firstRow, int lastRow) {
      	log.debug("Query for region parameters:" + region.toJson()+"\r\n,Page parameters:["+firstRow+","+lastRow+"]");
        if(firstRow<0 || firstRow>lastRow || lastRow<0){
            throw new RuntimeException("Digital is not logical");
        }
        if(lastRow-firstRow > MAX_RETURN_NUM){
            lastRow = firstRow + MAX_RETURN_NUM;
        }
        Map<String ,Object> param = new HashMap<String,Object>();
        param.put("region",region);
        param.put("firstRow",firstRow);
        param.put("rowSize",lastRow-firstRow);
        return queryList("region.queryRegions", param);
    }
    
    /**
     * 验证字段长度
     * @param region 需要验证的对象
     */
    private void validColumnLen(Region region){
		if(!StringUtils.isEmpty(region.getId()) && region.getId().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(region.getRegionCode()) && region.getRegionCode().length()>6){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{6},null);
        }
		if(!StringUtils.isEmpty(region.getRegionName()) && region.getRegionName().length()>30){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{30},null);
        }
		if(!StringUtils.isEmpty(region.getParentId()) && region.getParentId().length()>6){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{6},null);
        }
		if(!StringUtils.isEmpty(region.getStatus()) && region.getStatus().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(region.getRegionType()) && region.getRegionType().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(region.getPinyin()) && region.getPinyin().length()>20){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{20},null);
        }
		if(!StringUtils.isEmpty(region.getQuanpin()) && region.getQuanpin().length()>50){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{50},null);
        }
		if(!StringUtils.isEmpty(region.getLatitude()) && region.getLatitude().length()>25){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{25},null);
        }
		if(!StringUtils.isEmpty(region.getLongitude()) && region.getLongitude().length()>25){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{25},null);
        }
		if(!StringUtils.isEmpty(region.getRemark()) && region.getRemark().length()>300){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{300},null);
        }
        
        //数据来源不明的不能操作
        if(!baseDataUtils.isDataSourceAvailable(region.getDataSource())){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_DATASOURCE,null);
        }
    }
    
}
